package org.data;

import org.apache.commons.lang.ObjectUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;


public class Clean4 {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        //1 获取job对象
        Configuration conf = new Configuration();
        conf.set("mapred.textoutputformat.separator",",");
        conf.set("mapred.textoutputformat.ignoreseparator","true");
        Job job = Job.getInstance(conf);

        //2 关联Driver类
        job.setJarByClass(Clean4.class);

        //3 关联Mapper和Reducer类
        job.setMapperClass(Clean4Mapper.class);
        job.setReducerClass(Clean4Reducer.class);

        //4 设置Map的输入输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        //5	设置最终的输入输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        //6	设置输入输出路径
        FileInputFormat.setInputPaths(job,new Path("D:\\data\\out3\\part-r-00000"));
        FileOutputFormat.setOutputPath(job,new Path("D:\\data\\out4"));

        //7 提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result?0:1);

    }

}

class Clean4Mapper extends Mapper<Object, Text,Text,Text> {
    private Text outK = new Text();
    private Text outV = new Text();
    int door = 437507;
    int count = 0;
    Double min = 999999.9;

    static HashMap<String,String> _hashMap = new HashMap<>();

    protected void map(Object key, Text value, Context context) throws IOException,InterruptedException{

        String[] str = value.toString().split(",");

        String tmp_key = str[0]+","+str[1];
        String tmp_val = str[2];

        HashMap<String,String> hashMap = new HashMap<>();

        if (!_hashMap.isEmpty()) {
            if (!_hashMap.containsKey(tmp_key)) {
                hashMap.put(tmp_key, tmp_val);
                _hashMap.putAll(hashMap);
            } else {
                Double b1 = Double.valueOf(tmp_val);
                if (b1 < min) {
                    hashMap.put(tmp_key, String.valueOf(b1));
                    min = b1;
                    _hashMap.putAll(hashMap);
                }else {
                    hashMap.put(tmp_key, String.valueOf(min));
                    _hashMap.putAll(hashMap);
                }
            }
        }else {
            hashMap.put(tmp_key, tmp_val);
            _hashMap.putAll(hashMap);
        }
        count++;

        if (door == count){
            System.out.println(_hashMap.size());
            Iterator<String> iterator = _hashMap.keySet().iterator();
            while (iterator.hasNext()){
                String s1 = iterator.next();//key
                String s2 = _hashMap.get(s1);//value
                System.out.println("key"+s1);
                System.out.println(s2);
                if(s1!=null && s2!= null) {
                    outK.set(s1);
                    outV.set(s2);
                    context.write(outK, outV);
                }
            }//END WHILE
        }//END IF
    }//END MAP
}

class Clean4Reducer extends Reducer<Text,Text,Text, Text> {
    public void reduce(Text key, Iterable<Text> values,Context context) throws IOException,InterruptedException {
        Text content = new Text();
        for (Text value : values) {
            content.set(value);
        }
        context.write(key, content);
    }
}